package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.RuleScriptRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.rdb.model.rulescript.RuleScript;
import com.ndp.fb.util.SQLUtil;
import com.ndp.fb.util.StringUtil;

import java.util.List;

/**
 * Created by sisi.chen on 15-6-25.
 */
@Service
public class RuleScriptRdbServiceImpl extends DaoTemplate implements RuleScriptRdbService {
    private static String insertSql = "insert into `rule_script` "
            + "( `user_id`, `rule_name`, `rule_comment`, `rule_script`, `rule_engine`, "
            + " `log_level`, `data_cycle`, `version`, `is_run`, `create_time`, `ignore_databuild`,`snap_cycle`"
            +  ") "
            +  "values(?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    public Long save(final RuleScript ruleScript) {
        return saveResultId(ruleScript);
//        return saveResultId(insertSql, new PstAssign() {
//            @Override
//            public void setParam(PreparedStatement ps) throws SQLException {
//                ps.setString(1, ruleScript.getUserId());
//                ps.setString(2, ruleScript.getRuleName());
//                ps.setString(3, ruleScript.getRuleComment());
//                ps.setString(4, ruleScript.getRuleScript());
//                ps.setString(5, ruleScript.getRuleEngine());
//                ps.setObject(6, ruleScript.getLogLevel());
//                ps.setObject(7, ruleScript.getDataCycle());
//                ps.setString(8, ruleScript.getVersion());
//                ps.setObject(9, ruleScript.getIsRun());
//                ps.setObject(10, ruleScript.getCreateTime());
//                ps.setObject(11, ruleScript.getIgnoreDataBuild());
//                ps.setObject(12,ruleScript.getSnapCycle());
//            }
//        });
    }

    @Override
    public Pager<RuleScript> page(String userId, Integer offset, Integer limit) {
        String selSql = "select * from (select * from rule_script order by version desc) rule_script where user_id = ?   group by rule_name order by create_time desc";
        return findPager(selSql, new Object[]{userId}, offset, limit, DefaultRowMapper.autoRowMapper(RuleScript.class));
    }

    @Override
    public RuleScript findByUserIdAndId(String userId, Long id) {
        if(StringUtil.isNotEmpty(userId)){
            String selSql = "SELECT * FROM rule_script where id = ? and user_id = ? ";
            return findOne(selSql, new Object[]{id, userId} , DefaultRowMapper.autoRowMapper(RuleScript.class));
        }else{
            String selSql = "SELECT * FROM rule_script where id = ?";
            return findOne(selSql, new Object[]{id} , DefaultRowMapper.autoRowMapper(RuleScript.class));
        }

    }

    @Override
    public List<RuleScript> findByUserIdAndRuleName(String userId, String ruleName) {
        String selSql = "SELECT * FROM rule_script where user_id = ? and rule_name = ?";
        return getJdbcTemplate().query(selSql, new Object[]{userId, ruleName}, DefaultRowMapper.autoRowMapper(RuleScript.class));
    }

    @Override
    public void updateIsRunByIdsIn(int isRun, List<Long> ids) {
        String updateSql = "update rule_script set is_run = " + isRun + " where id in (" + SQLUtil.appendPlaceholder(ids.size()) +")";
        getJdbcTemplate().update(updateSql, ids.toArray());
    }


    @Override
    public List<RuleScript> getAllRuningRule() {
        String selSql = "SELECT * FROM rule_script where is_run = ?";
        return getJdbcTemplate().query(selSql, new Object[]{1}, DefaultRowMapper.autoRowMapper(RuleScript.class));
    }
}
